//
//  HDAroundVC.m
//  HDWuDaDaoServiceCenter
//
//  Created by HDNiuKuiming on 2017/4/12.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

@interface HDEatingObj : NSObject
@property (nonatomic, strong) NSString *hotel_id;
@property (nonatomic, strong) NSString *price;
@property (nonatomic, strong) NSString *list_img;
@property (nonatomic, strong) NSString *phone;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *address;

+ (instancetype)eatingObjWithDict:(NSDictionary *)dict;

@end

@implementation HDEatingObj

+ (instancetype)eatingObjWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict]; // 字典的快速赋值，需要特殊处理一下保留字段和未声明但是字典中有的字段
    }
    return self;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if([key isEqualToString:@"id"]) {
    }
}

@end
#import "HDAroundVC.h"
#import "HDEatingCell.h"
#import "HDHotelCell.h"

@interface HDAroundVC ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (nonatomic, strong) NSMutableArray *objArray;
@end

@implementation HDAroundVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableviewInit];
    
    _objArray = [NSMutableArray new];
    if (_listType == kList_Type_Eating)
        _nameLabel.text = @"周边餐饮";
    else if (_listType == kList_Type_Hotel)
        _nameLabel.text = @"周边酒店";
    
    [self aroundEatingRequest];
}

- (void)aroundEatingRequest
{
    HDRequestObj *loginObj = [[HDRequestObj alloc] init];
    NSString *urlPath = [NSString stringWithFormat:@"%@g=mapi&m=Discover&a=restaurant_info", HTTP_Request_URL_Prefix];
    if (_listType == kList_Type_Hotel)
        urlPath = [NSString stringWithFormat:@"%@g=mapi&m=Discover&a=hotel_info", HTTP_Request_URL_Prefix];
    
    NSDictionary *parameters = @{@"language": @"CHINESE"};
    
    [loginObj requestGetPath:urlPath Parameter:parameters withVC:self timeout:5];
    loginObj.myDataBlock = ^(NSDictionary *dataDic){
        
        NSString *loginCode = dataDic[@"status"];
        if (loginCode.integerValue == 1) {
            NSArray *objArray = dataDic[@"data"];
            
            [_objArray removeAllObjects];
            for (NSInteger i=0;i<objArray.count;i++) {
                NSDictionary *myDic = objArray[i];
                HDEatingObj *eatingObj = [HDEatingObj eatingObjWithDict:myDic];
                [_objArray addObject:eatingObj];
            }
            [_myTableView reloadData];
        }
        else {
        }
    };
}

#pragma mark
#pragma mark -------- TableView Mehods --------

- (void)tableviewInit {
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.rowHeight = 80;
    _myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _objArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID = nil;
    if (_listType == kList_Type_Hotel) {
        cellID = @"HDEatingCell";
        HDHotelCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"HDHotelCell" owner:self options:nil] lastObject];
            HDEatingObj *obj = _objArray[indexPath.row];
            cell.nameLabel.text = obj.title;
            cell.addressLabel.text = obj.address;
            cell.priceLabel.text = obj.price;
            [cell.imgView sd_setImageWithURL:[NSURL URLWithString:obj.list_img] placeholderImage:[UIImage imageNamed:@"search_eating"]];
            
            return cell;
        }
    }
    else {
        cellID = @"HDEatingCell";
        HDEatingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"HDEatingCell" owner:self options:nil] lastObject];
            HDEatingObj *obj = _objArray[indexPath.row];
            cell.nameLabel.text = obj.title;
            cell.addressLabel.text = obj.address;
            cell.priceLabel.text = obj.price;
            [cell.imgView sd_setImageWithURL:[NSURL URLWithString:obj.list_img] placeholderImage:[UIImage imageNamed:@"search_eating"]];

            return cell;
        }
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark
#pragma mark -------- Dealloc Methods ---------

- (IBAction)backAction:(id)sender {
    [super back];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [_objArray removeAllObjects];
    _objArray = nil;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
